package com.xxl.job.executor.utils;

/**
 * @author xie xin
 * @date 2025/1/10 22:27
 */
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.util.Objects;

@Component
public class SftpUploadUtil implements ApplicationRunner {
    @Resource
    private Environment environment;
    public static String sftpHost;
    public static String sftpUsername;
    public static String sftpPassword;
    public static Integer sftpPort;
    @Override
    public void run(ApplicationArguments args) {
        sftpHost = environment.getProperty("xxl.job.sftp.username");
        sftpUsername = environment.getProperty("xxl.job.sftp.password");
        sftpPassword = environment.getProperty("xxl.job.sftp.host");
        sftpPort = Integer.valueOf(Objects.requireNonNull(environment.getProperty("xxl.job.sftp.port")));
    }
    public static void uploadFile (String localFilePath, String remoteFilePath) {
//        String localFilePath = "/path/to/local/file.txt";
//        String remoteFilePath = "/path/to/remote/file.txt";
        Session session = null;
        ChannelSftp channelSftp = null;
        try {
            JSch jsch = new JSch();
            session = jsch.getSession(sftpUsername, sftpHost, sftpPort);
            session.setPassword(sftpPassword);

            // Avoid asking for key confirmation
            session.setConfig("StrictHostKeyChecking", "no");

            session.connect();
            System.out.println("Connected to SFTP server");

            channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();
            System.out.println("SFTP channel opened and connected");

            try (FileInputStream fis = new FileInputStream(localFilePath)) {
                channelSftp.put(fis, remoteFilePath);
                System.out.println("File uploaded successfully to " + remoteFilePath);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (channelSftp != null) {
                channelSftp.exit();
            }
            if (session != null) {
                session.disconnect();
            }
        }
    }
    public static void main(String[] args) {
        String sftpHost = "your.sftp.server";
        int sftpPort = 22;
        String sftpUser = "your_username";
        String sftpPass = "your_password";
        String localFilePath = "/path/to/local/file.txt";
        String remoteFilePath = "/path/to/remote/file.txt";

        Session session = null;
        ChannelSftp channelSftp = null;

        try {
            JSch jsch = new JSch();
            session = jsch.getSession(sftpUser, sftpHost, sftpPort);
            session.setPassword(sftpPass);

            // Avoid asking for key confirmation
            session.setConfig("StrictHostKeyChecking", "no");

            session.connect();
            System.out.println("Connected to SFTP server");

            channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();
            System.out.println("SFTP channel opened and connected");

            try (FileInputStream fis = new FileInputStream(localFilePath)) {
                channelSftp.put(fis, remoteFilePath);
                System.out.println("File uploaded successfully to " + remoteFilePath);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (channelSftp != null) {
                channelSftp.exit();
            }
            if (session != null) {
                session.disconnect();
            }
        }
    }
}
